<template>
  <div class="app-container">
    <div class="head">
      <img src="@/assets/img/Layout/position.png" />
      <p class="see-block">账号绑定</p>
    </div>
    <div class="view-container">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            class="btn-success"
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['system:switchAccountNumber:insert']"
          >新建</el-button>
        </el-col>
      </el-row>
      <el-table v-loading="loading" :data="userList" border>
        <el-table-column label="序号" type="index" align="center" width="50">
           <template slot-scope="scope">
            <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
          </template>
        </el-table-column>
        <el-table-column label="用户账号" prop="userName" align="center" />
        <el-table-column label="登录账号" prop="account" align="center" />
        <el-table-column label="账号说明" prop="remark" align="center" />
        <el-table-column label="操作" align="center" class="150">
          <template slot-scope="scope" v-if="scope.row.roleId !== 1">
            <!-- <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:role:edit']"
            >编辑</el-button> -->
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:switchAccountNumber:deleteById']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <!-- 添加或编辑账号绑定 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" top="50px" append-to-body center>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="账号" prop="account" v-if="form.id == undefined">
          <el-input v-model="form.account" placeholder="请输入账号" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" type="password" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="账号说明" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入账号说明"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" class="btn-success mr20 width100" @click="submitForm">确 定</el-button>
        <el-button class="width100" @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listUserBind, getUserBind, addUserBind, editUserBind, delUserBind } from "@/api/system/setting/userbind/userbind";

export default {
  name: "userList",
  data() {
    return {
      loading: true,
      total: 0,
      userList: [],
      title: "",
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        account: [
          { required: true, message: "账号不能为空", trigger: "blur" }
        ],
        password: [
          { required: true, message: "密码不能为空", trigger: "blur" }
        ],
        remark: [
          { required: true, message: '备注不能为空', trigger: "blur" }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 获取账号绑定列表
    getList() {
      this.loading = true
      listUserBind(this.addDateRange(this.queryParams)).then(res => {
          this.userList = res.rows
          this.total = res.total
          this.loading = false
        }
      )
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        account: undefined,
        password: undefined,
        remark: undefined
      }
      this.resetForm("form")
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 新建按钮
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "新建账号绑定"
    },
    // 编辑按钮
    handleUpdate(row) {
      this.reset()
      getUserBind(row.id).then(res => {
        this.open = true
        this.title = "编辑账号绑定"
        this.form = res.data
      })
    },
    // 提交按钮
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            editUserBind(this.form).then(res => {
              this.$modal.msgSuccess("编辑成功")
              this.open = false
              this.getList()
            })
          } else {
            // console.log(this.form)
            addUserBind(this.form).then(res => {
              this.$modal.msgSuccess("新建成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    // 删除按钮
    handleDelete(row) {
      this.$modal.confirm('是否确认删除数据？').then(function() {
        return delUserBind(row.id)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    }
  }
}
</script>
